//
//  YYEMyTopicViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/4.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYEMyTopicViewController.h"
#import "YYEWrongRecordViewController.h"
#import "YYETopicRecordViewController.h"
#import "SPPageMenu.h"

@interface YYEMyTopicViewController () <SPPageMenuDelegate,UIScrollViewDelegate>

@property (nonatomic,strong) SPPageMenu *pageMenu;
@property (nonatomic,strong) UIScrollView *contentSV;

@end

@implementation YYEMyTopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的习题";
    
    [self configUI];
    

    
}

- (void)configUI {
    
    NSArray *datas = @[@"当前考试习题",@"考试答题记录"];
    
    self.pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
//    self.pageMenu.itemPadding = SCREEN_WIDTH / 4;
    [self.pageMenu setWidth:SCREEN_WIDTH / 2 forItemAtIndex:0];
    self.pageMenu.selectedItemTitleColor = GeneralBlueColor;
    self.pageMenu.unSelectedItemTitleColor = LineColor;
    self.pageMenu.tracker.image = [UIImage imageNamed:@"bottomLine"];
    self.pageMenu.dividingLine.backgroundColor = GeneralBackColor;
    self.pageMenu.needTextColorGradients = NO;
    self.pageMenu.delegate = self;
    self.pageMenu.permutationWay = SPPageMenuPermutationWayNotScrollEqualWidths;
    [self.view addSubview:self.pageMenu];
    
    YYEWrongRecordViewController *wrongVC = [[YYEWrongRecordViewController alloc]init];
    YYETopicRecordViewController *topicVC = [[YYETopicRecordViewController alloc]init];
    [self addChildViewController:wrongVC];
    [self addChildViewController:topicVC];
    
    self.contentSV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT - 40 - 64)];
    self.contentSV.delegate = self;
    self.contentSV.pagingEnabled = YES;
    self.contentSV.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.contentSV];
    
    for (NSInteger i = 0; i < self.childViewControllers.count; i ++) {
        
        YYEBaseViewController *baseVC = self.childViewControllers[i];
        [self.contentSV addSubview:baseVC.view];
        
        baseVC.view.frame = CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40 - 64);
        self.contentSV.contentOffset = CGPointMake(SCREEN_WIDTH * i, 0);
        self.contentSV.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
        
    }
    self.pageMenu.bridgeScrollView = self.contentSV;
    [self.pageMenu setItems:datas selectedItemIndex:0];
    
}

#pragma mark -- SPPageMenuDelegate
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedAtIndex:(NSInteger)index {
    
    self.contentSV.contentOffset = CGPointMake(SCREEN_WIDTH * index, 0);
    
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    
    if (labs(toIndex - fromIndex) >= 2) {
        [self.contentSV setContentOffset:CGPointMake(SCREEN_WIDTH * toIndex, 0) animated:NO];
    }else {
        [self.contentSV setContentOffset:CGPointMake(SCREEN_WIDTH * toIndex, 0) animated:YES];
    }
    
    UIViewController *targetViewController = self.childViewControllers[toIndex];
    // 如果已经加载过，就不再加载
    if ([targetViewController isViewLoaded]) return;
    
    targetViewController.view.frame = CGRectMake(SCREEN_WIDTH * toIndex, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40);
    [self.contentSV addSubview:targetViewController.view];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
